package org.ytor.core.sqlflow.dsl.template;

import org.ytor.core.sqlflow.dsl.expr.node.support.FunctionCallNode;
import org.ytor.core.sqlflow.dsl.eval.EvalContext;

import java.util.List;

/**
 * created by yangtong on 2025/8/7 12:10:07
 * <br/>
 * 合并上下文，为 for: 循环中的 index/item 提供作用域隔离
 */
public class MergedEvalContext implements EvalContext {
    private final EvalContext parent;
    private final EvalContext child;

    public MergedEvalContext(EvalContext parent, EvalContext child) {
        this.parent = parent;
        this.child = child;
    }

    @Override
    public Object getVariable(String name) {
        Object val = child.getVariable(name);
        return val != null ? val : parent.getVariable(name);
    }

    @Override
    public Object callFunc(FunctionCallNode node, List<Object> args) {
        return parent.callFunc(node, args); // 函数调用统一走主上下文
    }
}
